﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using System.IO;
using System.Xml;

using Components.Base.ComponentInterface;


//includes for seans graphics compoents
using SunXNA.RainbowXNA;
using SunXNA;
using NeedsMoreNinja.EventManager;

//
using Sound;
using Input;

namespace Components.MenuSystem
{
    class Menu : ComponentInterface
    {
        CRainbowXNA.TextureObjectHandleType m_Background;//handle for the textureObject
        CRainbowXNA.TextureObjectHandleType m_ButtonOne;//handle for the textureObject
        CRainbowXNA.TextureObjectHandleType m_ButtonTwo;//handle for the textureObject

        private Vector2 m_BackgroundPos = new Vector2(0, 0);
        private Rectangle m_BackgroundRect = new Rectangle(0, 0, 1280, 720);

        //get initialised by xml
        private Vector2 m_ButtonOnePosition;
        private Vector2 m_ButtonTwoPosition;
        private Rectangle m_LayerRect = new Rectangle(0, 0, 512, 64);

        private Boolean m_ButtonOneDrawn = true;

        public override void draw2D()
        {
            //render background
            Singleton<CRainbowXNA>.Instance.RenderTexture(m_Background, ref m_BackgroundPos, ref m_BackgroundRect);

            if (m_ButtonOneDrawn)
            {
                //render extra layer
                Singleton<CRainbowXNA>.Instance.RenderTexture(m_ButtonOne, ref m_ButtonOnePosition, ref m_LayerRect);
            }
            else
            {
                Singleton<CRainbowXNA>.Instance.RenderTexture(m_ButtonTwo, ref m_ButtonTwoPosition, ref m_LayerRect);
            }
        }

        public override void draw3D()
        {

        }

        public void toggleButtonDraw()
        {
            if (m_ButtonOneDrawn)
            {
                m_ButtonOneDrawn = false;
            }
            else
            {
                m_ButtonOneDrawn = true;
            }
        }

        public override void update(GameTime time)
        {

        }

        public override void loadContent(ContentManager content,String xmlFile)
        {
            StreamReader reader = new StreamReader(xmlFile);
            XmlDocument machineState = new XmlDocument();
            machineState.LoadXml(reader.ReadToEnd());
            reader.Close();

            //now start reading from the xml
            XmlNode root = machineState.SelectSingleNode("Menu");

            //gets all the nodes
            XmlNodeList nodes = root.ChildNodes;
            //loads the background!
            m_Background = Singleton<SunXNA.RainbowXNA.CRainbowXNA>.Instance.LoadTexture(nodes[0].FirstChild.InnerText.Trim());

            //now get the buttons
            XmlNodeList buttons =  nodes[1].ChildNodes;

            //the first button!!!
            XmlNodeList stuff = buttons[0].ChildNodes;

            //button texture
            m_ButtonOne = Singleton<SunXNA.RainbowXNA.CRainbowXNA>.Instance.LoadTexture(stuff[0].InnerText.Trim());

            //posX
            m_ButtonOnePosition.X = Convert.ToInt32(stuff[1].FirstChild.InnerText.Trim());
            //posY
            m_ButtonOnePosition.Y = Convert.ToInt32(stuff[2].FirstChild.InnerText.Trim());

            //the second button!!!
            stuff = buttons[1].ChildNodes;

            m_ButtonTwo = Singleton<SunXNA.RainbowXNA.CRainbowXNA>.Instance.LoadTexture(stuff[0].InnerText.Trim());

            //posX
            m_ButtonTwoPosition.X = Convert.ToInt32(stuff[1].FirstChild.InnerText.Trim());
            //posY
            m_ButtonTwoPosition.Y = Convert.ToInt32(stuff[2].FirstChild.InnerText.Trim());
        }

        public override void unloadContent(ContentManager content)
        {

        }
    }
}
